{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 补充知识对数概念：a^n=b<==>loga(b)=n【a:为底数，b：真数，n:对数】对于真数要求：b>0且b不等于1\n",
    "* math.log(x) 就相当于数学中的ln(x)，x>0，求底数为e的对数，e = 2.718281828459；\n",
    "* math.log10(x) 就相当于数学中的lg(x)，x>0，求底数为10的对数；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 从控制台上接受参数 input（‘输出文字’）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字：2\n",
      "0.6931471805599453\n",
      "请输入数字：3\n",
      "1.0986122886681098\n",
      "请输入数字：\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "could not convert string to float: ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-7506e2c20bd9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0minput_number\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;34m'q'\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m         \u001b[1;32mbreak\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m     \u001b[0mresult\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput_number\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: could not convert string to float: "
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    input_number=input('请输入数字：')\n",
    "    if input_number=='q' :\n",
    "        break;\n",
    "    result=math.log(float(input_number))\n",
    "    print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 异常捕捉try:  except :  finally:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字：2\n",
      "0.6931471805599453\n",
      "请输入数字：3\n",
      "1.0986122886681098\n",
      "请输入数字：4\n",
      "1.3862943611198906\n",
      "请输入数字：4\n",
      "1.3862943611198906\n",
      "请输入数字：0\n",
      "ValueError: input must > 0\n",
      "请输入数字：1\n",
      "0.0\n",
      "请输入数字：q\n"
     ]
    }
   ],
   "source": [
    " for i in range(10):\n",
    "    try :\n",
    "\n",
    "        input_number=input('请输入数字：')\n",
    "        if input_number=='q' :\n",
    "            break;\n",
    "        result=math.log(float(input_number))\n",
    "        print(result)\n",
    "    except ValueError :\n",
    "        print ('ValueError: input must > 0')\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字：1\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "float division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-eaf6e3feeee6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0minput_number\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;34m'q'\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m             \u001b[1;32mbreak\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m         \u001b[0mresult\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput_number\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mZeroDivisionError\u001b[0m: float division by zero"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    try :\n",
    "\n",
    "        input_number=input('请输入数字：')\n",
    "        if input_number=='q' :\n",
    "            break;\n",
    "        result=1/math.log(float(input_number))\n",
    "        print(result)\n",
    "    except ValueError :\n",
    "        print ('ValueError: input must > 0')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 如何依次捕捉异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字：1\n",
      "ZeroDivisionError: input must is not 0\n",
      "请输入数字：0\n",
      "ValueError: input must > 0\n",
      "请输入数字：1\n",
      "ZeroDivisionError: input must is not 0\n",
      "请输入数字：2\n",
      "1.4426950408889634\n",
      "请输入数字：3\n",
      "0.9102392266268373\n",
      "请输入数字：4\n",
      "0.7213475204444817\n",
      "请输入数字：5\n",
      "0.6213349345596119\n",
      "请输入数字：5\n",
      "0.6213349345596119\n",
      "请输入数字：6\n",
      "0.5581106265512472\n",
      "请输入数字：q\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    try :\n",
    "\n",
    "        input_number=input('请输入数字：')\n",
    "        if input_number=='q' :\n",
    "            break;\n",
    "        result=1/math.log(float(input_number))\n",
    "        print(result)\n",
    "    except ValueError :\n",
    "        print ('ValueError: input must > 0')\n",
    "    except ZeroDivisionError :\n",
    "        print ('ZeroDivisionError: input must is not 0')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 多个异常完整是什么"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字：1\n",
      "ZeroDivisionError: input must is not 0\n",
      "最后执行\n",
      "请输入数字：2\n",
      "1.4426950408889634\n",
      "最后执行\n",
      "请输入数字：3\n",
      "0.9102392266268373\n",
      "最后执行\n",
      "请输入数字：4\n",
      "0.7213475204444817\n",
      "最后执行\n",
      "请输入数字：5\n",
      "0.6213349345596119\n",
      "最后执行\n",
      "请输入数字：6\n",
      "0.5581106265512472\n",
      "最后执行\n",
      "请输入数字：8\n",
      "0.48089834696298783\n",
      "最后执行\n",
      "请输入数字：0\n",
      "ValueError: input must > 0\n",
      "最后执行\n",
      "请输入数字：q\n",
      "最后执行\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    try :\n",
    "\n",
    "        input_number=input('请输入数字：')\n",
    "        if input_number=='q' :\n",
    "            break;\n",
    "        result=1/math.log(float(input_number))\n",
    "        print(result)\n",
    "    except ValueError :\n",
    "        print ('ValueError: input must > 0')\n",
    "    except ZeroDivisionError :\n",
    "        print ('ZeroDivisionError: input must is not 0')\n",
    "    except Exception :\n",
    "        print('other error!')\n",
    "    finally :\n",
    "        print('最后执行')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 自定义异常【需要】继承自定义类\n",
    "* raise 抛出异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Temp_Error(ValueError):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "temp=['guochao','is','good']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入文字guochao\n",
      "请输入文字is\n",
      "请输入文字g\n"
     ]
    },
    {
     "ename": "Temp_Error",
     "evalue": "Invalid input:g",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTemp_Error\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-26-a4fc7d147b55>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m         \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mcurent_temp\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m  \u001b[0mtemp\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mTemp_Error\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Invalid input:{}'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcurent_temp\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTemp_Error\u001b[0m: Invalid input:g"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    curent_temp=input('请输入文字')\n",
    "    if(curent_temp=='q'):\n",
    "        break\n",
    "    if curent_temp not in  temp :\n",
    "        raise Temp_Error('Invalid input:{}'.format(curent_temp))\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 完成异常抛出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "异常报错了！\n",
      "结束了\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    result=1/float(input())\n",
    "except Exception :\n",
    "    print(\"异常报错了！\")\n",
    "finally:\n",
    "    print(\"结束了\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
